﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Practices.Unity;
using ePlanet.SingleWell.Services;
using ePlanet.SingleWell.Models;
using ePlanet.ResourceRequest.Models;
using ePlanet.ResourceRequest.Services;
using System.Data;
using DreamBuilder.Core.Container;

namespace Web.Views.ResourceRequestManage
{
    public partial class ResourceRequestWellView : PageBase
    {
        [Dependency]
        public ISingleWellService m_ISingleWellService
        {
            get;
            set;
        }

        [Dependency]
        public IResourceRequestService m_IResourceRequestService
        {
            get;
            set;
        }

        [Dependency]
        public IAttachmentService m_IAttachmentService
        {
            get;
            set;
        }

        private Web.SystemLogin.Models.User m_CurrentUser
        {
            get
            {
                return (Web.SystemLogin.Models.User)Session["CurrentUser"];
            }
        }

        private string m_RequestID
        {
            get;
            set;
        }

        private bool _canView;
        private bool _canDown;

        protected void Page_Load(object sender, EventArgs e)
        {
            
            try
            {
                if (string.IsNullOrEmpty(Request.QueryString["ID"]))
                {
                    throw new Exception("参数ID不能为空");
                }
                else
                {
                    this.m_RequestID = Request.QueryString["ID"];
                }

                if (!IsPostBack)
                {
                    bool canView = false;
                    bool canDownload = false;

                    //绑定数据，判断数据是否可以下载后者下载
                    ResourceRequestModel oResourceRequestModel = this.m_IResourceRequestService.GetResourceRequestModelByID(this.m_RequestID);
                    if (oResourceRequestModel == null || oResourceRequestModel.RESOURCEREQUEST.Rows.Count == 0)
                    {
                        throw new Exception("获取的资源申请对象为空");
                    }
                    SingleWellModel oSingleWellModel = this.m_ISingleWellService.GetSingleWellModel(oResourceRequestModel.RESOURCEREQUEST[0].WELLDATAID);
                    if (oSingleWellModel == null || oSingleWellModel.SINGLEWELL.Rows.Count == 0)
                    {
                        throw new Exception("获取的单井对象为空");
                    }
                    if (this.m_IResourceRequestService.GetPrivilege(m_RequestID, m_CurrentUser.User_ID, ref canView, ref canDownload))
                    {
                        _canView = oResourceRequestModel.RESOURCEREQUEST[0].CANVIEW == 1;
                        _canDown = oResourceRequestModel.RESOURCEREQUEST[0].CANDOWNLOAD == 1;

                    }

                    //throw new Exception("您没有查看此数据的权限");
                    this.BindSingleData(oSingleWellModel);
                    this.BindWellDetail(oSingleWellModel);
                    this.BindAttachment(oSingleWellModel);

                }
            }
            catch (Exception ex)
            {
                Alert(ex.Message);
            }
        }

        #region 私有方法
        private void BindData()
        {
            SingleWellModel model = this.m_ISingleWellService.GetSingleWellModel(this.m_RequestID);
            if (model != null && model.SINGLEWELL.Rows.Count > 0)
            {
                //绑定数据
            }
        }

        #region 私有方法
        private void BindAttachment(SingleWellModel oModel)
        {
            DataTable oDataTable = null;
            this.m_IAttachmentService.Initialize("SingleWell");
            List<AttachmentModel> modelList = this.m_IAttachmentService.GetAttachmentModelByObjectID(oModel.SINGLEWELL[0].DATAID);
            if (modelList != null && modelList.Count > 0)
            {
                oDataTable = new DataTable();
                oDataTable.Columns.Add("attachmentid");
                oDataTable.Columns.Add("attachmentname");
                oDataTable.Columns.Add("uploadtime");
                oDataTable.Columns.Add("attachmentsize");

                foreach (AttachmentModel model in modelList)
                {
                    DataRow dr = oDataTable.NewRow();
                    dr["attachmentid"] = model.AttachmentID;
                    dr["attachmentname"] = model.AttachmentName;
                    dr["uploadtime"] = model.UpdateTime;
                    dr["attachmentsize"] = (model.AttachmentSize / (1000 * 1000) > 0) ? (Math.Round((float)model.AttachmentSize / (1000 * 1000), 3) + "MB") : Math.Round((float)model.AttachmentSize / (1000)) + "KB";

                    oDataTable.Rows.Add(dr);
                }
                //时间：2013年3月24日 12:45:51
                //added by jiejiep
                //绑定附件
                gv_AttachMent.DataSource = oDataTable;
                gv_AttachMent.DataBind();
            }
        }

        protected void gv_AttachMent_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRow row = ((DataRowView)e.Row.DataItem).Row;
                e.Row.Cells[0].Text = row["attachmentname"].ToString();
                ///浏览
                if (_canView)
                {
                    //e.Row.Cells[0].Text = string.Format("<a target='_blank' href='../../WebDoc/WebDoc.aspx?AttachmentID={0}&AttachmentDBHelperString=SingleWell'>{1}</a>", row["attachmentid"], row["attachmentname"]);
                    e.Row.Cells[3].Text = string.Format("<a target='_blank' href='../../WebDoc/WebDoc.aspx?AttachmentID={0}&AttachmentDBHelperString=SingleWell'>浏览</a>", row["attachmentid"]);
                }
                else
                {
                    
                    e.Row.Cells[3].Text = "";
                }
                ///下载
                if (_canDown)
                {
                    //e.Row.Cells[0].Text = string.Format("<a target='_blank' href='../../WebDoc/WebDoc.aspx?AttachmentID={0}&AttachmentDBHelperString=SingleWell'>{1}</a>", row["attachmentid"],row["attachmentname"]);
                    e.Row.Cells[4].Text = string.Format("<a target='_blank' href='../../WebDoc/FileDownloadHandler.ashx?AttachmentID={0}&AttachmentDBHelperString=SingleWell'>下载</a>", row["attachmentid"]);
                }
                else
                {
                    e.Row.Cells[4].Text = "";
                }
                
            }
        }

        private bool BindWellDetail(SingleWellModel oModel)
        {
            try
            {
                DataTable oDataTable = oModel.WELLDATADETAIL as DataTable;
                this.gv_SingleWellDetail.DataSource = oDataTable;
                this.gv_SingleWellDetail.DataBind();
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void gv_SingleWellDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRow row = ((DataRowView)e.Row.DataItem).Row;
                
                if (_canView || _canDown)
                {
                    e.Row.Cells[4].Text = string.Format("<a target='_blank' href='../../WebDoc/WebDoc.aspx?DetailID={0}&PageNum={1}&AttachmentDBHelperString=SingleWell'>{2}</a>",
                        row["DETAILID"],row["PAGENUM"],row["PAGENUM"]);
                }
            }
        }

        private bool BindSingleData(SingleWellModel oModel)
        {
            try
            {
                SingleWellModel.SINGLEWELLRow singleRow = (SingleWellModel.SINGLEWELLRow)oModel.SINGLEWELL.Rows[0];
                if (!singleRow.IsA0Null())
                {
                    this.txtA0.Text = singleRow.A0.ToString();
                }
                if (!singleRow.IsA3Null())
                {
                    this.txtA3.Text = singleRow.A3.ToString();
                }
                if (!singleRow.IsA4Null())
                {
                    this.txtA4.Text = singleRow.A4.ToString();
                }
                if (!singleRow.IsARCHIVECODENull())
                {
                    //this.txtArchiveCode.Text = singleRow.ARCHIVECODE;
                }
                if (!singleRow.IsBEGINDATENull())
                {
                    this.txtBEGINDATE.Text = singleRow.BEGINDATE.ToShortDateString();
                }
                if (!singleRow.IsBEGINWORKDATENull())
                {
                    this.txtBeginWorkDate.Text = singleRow.BEGINWORKDATE.ToShortDateString();
                }
                if (!singleRow.IsFINISHDATENull())
                {
                    this.txtFINISHDATE.Text = singleRow.FINISHDATE.ToShortDateString();
                }
                if (!singleRow.IsFINISHWORKDATENull())
                {
                    this.txtFinishWorkDate.Text = singleRow.FINISHWORKDATE.ToShortDateString();
                }
                if (!singleRow.IsQUANZONGCODENull())
                {
                    //this.txtQuanzongCode.Text = singleRow.QUANZONGCODE;
                }
                if (!singleRow.IsWELLDEPTHNull())
                {
                    this.txtWellDepth.Text = singleRow.WELLDEPTH.ToString();
                }
                if (!singleRow.IsDATAFILENAMENull())
                {
                    this.txtDatafileName.Text = singleRow.DATAFILENAME;
                }
                //if (!singleRow.IsSTATUSIDNull())
                //{
                //    this.hdfCurrentStatusID.Value = singleRow.STATUSID.ToString();//赋值当前状态
                //}
                if (!singleRow.IsWELLTYPENull())
                {
                    //this.txtWellDepth.Text = (int)singleRow.WELLTYPE;
                }

                if (!singleRow.IsWORKMODENull())
                {
                    this.txtWorkMode.Text = singleRow.WORKMODE;
                }
                if (!string.IsNullOrEmpty(singleRow.WELLCODE))
                {
                    this.txtWellCode.Text = singleRow.WELLCODE;
                }

                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
        #endregion
    }
}